const app = getApp();
import { hexMD5 } from "../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videlit: {},
    op: {},
    getback: true,
    lottery_bj: true,
    CUSTOMER_ID: '',
    jiami: '',
    openid: '',
    session_key: '',

  },
  go() {
    console.log("44444444444");


    wx.reLaunch({
      url: '/pages/xianzuimeiyinlurenliebiao/xianzuimeiyinlurenliebiao'
    })



  },


  pull() {

    wx.request({
      url: app.globalData.interface+'/xianPost/API_6309_XiAnBeautifulGuideEncourageCheck',
      method: 'POST',
      data: {
        OPEN_ID: app.globalData.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res擦互训', res);
        wx.hideLoading()
        if (res.data.result == "success") {

          this.toupiao()

        }else{

          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后   
                console.log('用户点击确定')
                wx.reLaunch({
                  url: '../../pages/newquanyizhuce/newquanyizhuce'
                })
              } else { //这里是点击了取消以后
                console.log('用户点击取消')
                wx.reLaunch({
                  url: '../../pages/newquanyizhuce/newquanyizhuce'
                })
              }
            }
          })
        }

      },
    })
  },





 



  toupiao() {

    this.setData({

      CUSTOMER_ID: app.globalData.CUSTOMER_ID
    });

    if (this.data.videlit.DANCE_ID && app.globalData.openid) {
      wx.request({
        url: app.globalData.interface+'/xianPost/API_6303_XiAnBeautifulGuideEncourage',
        method: 'POST',
        data: {
          DANCE_ID: this.data.videlit.DANCE_ID, //舞蹈id
          KEY: 'xianjgjKey9999',
          CUSTOMER_ID: app.globalData.CUSTOMER_ID,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key:this.data.session_key,
          OPEN_UUID:this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);
          wx.hideLoading()
          const that = this
          if (res.data.result == "success") {
            this.setData({
              lottery_bj: false
            })

            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后   
                  console.log('用户点击确定')
                  wx.reLaunch({
                    url: '../../pages/xianzuimeiyinlurenliebiao/xianzuimeiyinlurenliebiao'
                  })
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                  wx.reLaunch({
                    url: '../../pages/xianzuimeiyinlurenliebiao/xianzuimeiyinlurenliebiao'
                  })
                }
              }
            })
          } else {
     
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后   
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1,
                  })
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })
    }
  },

  danceDetail(DAN = '', EMP = '') {
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.interface+'/xianPost/API_6307_XiAnBeautifulGuideEncourageDetail',
      method: 'POST',
      data: {
        DANCE_ID: DAN, //id
        EMPLOYEE_ID: EMP, //  
        OPEN_ID: app.globalData.openid, //用户OPEN_ID
        session_key:this.data.session_key,
        OPEN_UUID:this.data.jiami
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res擦互训', res);
        wx.hideLoading()
        if (res.data.result == "success") {

          this.setData({
            videlit: res.data.data
          })

        }


      },
      fail: (res => {
        wx.hideLoading()
      })
    })


  },
















  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    let that = this

    console.log('转发1', options);
    that.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
      op: options,
    })

    if (that.data.jiami == ''||that.data.session_key == '') {
      

  
      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求
            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                } else {

                  that.setData({
                    openid: res.data.openid,
                    jiami: hexMD5(`${res.data.openid.substring(0,20)}${res.data.session_key.substring(0,10)}`),
                    OPEN_ID:res.data.openid,
                    session_key: res.data.session_key,
                  })
                  console.log(that.data.jiami);
                  console.log(that.data.OPEN_ID);
                  console.log(that.data.session_key);
                }
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    
    
  }else{
    console.log('成功');
  }
    //   if(options.open == 1){
    //     wx.hideHomeButton()  //隐藏左上角首页按钮
    //  }

    const {
      info
    } = options
    const copy = JSON.parse(info)

    that.setData({
      videlit: copy
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    console.log('666666666666666', this.data.videlit);
    wx.showShareMenu({
      withShareTicket: true,
      //设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
      menus: ["shareAppMessage", "shareTimeline"]
    })

    console.log('传参', this.data.op);
    const p = JSON.parse(this.data.op.id)

    console.log(p);

    //  if(p){
    //    this.setData({
    //      getback:false
    //    })
    //  }
    console.log('lll', p.DANCE_ID);
    console.log('8888888', p.EMPLOYEE_ID);
    this.danceDetail(p.DANCE_ID, p.EMPLOYEE_ID)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log('4444444444');
    const cd = {
      DANCE_ID: this.data.videlit.DANCE_ID,
      EMPLOYEE_ID: this.data.videlit.EMPLOYEE_ID
    }
    const idd = JSON.stringify(cd)
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    return {
      title: '西安市“西安最美引路人”,快来投票吧！', // 默认是小程序的名称(可以写slogan等)
      desc: '西安市“西安最美引路人”邀请您来投票啦！', // 默认是当前页面，必须是以‘/’开头的完整路径
      path: `/pages/xianzuimeiyinlurentoupiao/xianzuimeiyinlurentoupiao?id=${idd}`,
      imageUrl: 'https://jiagaojie.oss-cn-beijing.aliyuncs.com/image/zuimeiyinluyuangong.jpg', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          console.log('444444444444455555555');
        }
      },
      fail: function () {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
        }
      },
      complete: {
        // 转发结束之后的回调（转发成不成功都会执行）
      }
    }

  },

})